CC := arm-none-eabi-gcc
AS := arm-none-eabi-as
LD := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy

CFLAGS := -Iinclude -Idrivers/include -std=gnu99 -Os -mthumb -mcpu=cortex-a9 -fno-builtin-printf -fno-strict-aliasing -fno-builtin-memcpy -mno-unaligned-access -DPRINTF_DISABLE_SUPPORT_FLOAT=1 \
	-DCONFIG_MTK_PMIC_NEW_ARCH -DCONFIG_MTK_PMIC_CHIP_MT6353 -DCONFIG_U3_PHY_AHB_SUPPORT -DCONFIG_LGE_PM -DCONFIG_PROJECT_PHY \
	-DCONFIG_MTK_UART_USB_SWITCH
LDFLAGS := -T linker.x -nodefaultlibs -nostdlib

BUILD_DIR := ./build

TARGET := payload

C_SRC = main.c ../lkp/libc.c libc/memmove.c libc/memcmp.c libc/memchr.c libc/strchr.c libc/strnlen.c libc/strrchr.c libc/atoi.c libc/ctype.c \
	drivers/usb/mtk-phy-asic.c drivers/usb/mtk-phy.c drivers/usb/mtk-phy-ahb.c \
	drivers/drivers.c \
	drivers/u3hal/mu3d_hal_usb_drv.c \
	#drivers/pmic/upmu_common.c drivers/pmic/pmic.c drivers/pmic/pwrap_hal_v1.c drivers/pmic/mt_pmic_wrap.c \
	#drivers/clk/clk-mt6755.c \
	


ASM_SRC = start.S jump.S

OBJ = $(C_SRC:%.c=$(BUILD_DIR)/%.o) $(ASM_SRC:%.S=$(BUILD_DIR)/%.o)
DEP = $(OBJ:%.o=%.d)

$(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf
	$(OBJCOPY) -O binary $^ $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJ)
	$(LD) -o $@ $^ $(LDFLAGS) -lgcc

-include $(DEP)

$(BUILD_DIR)/%.o: %.c
	mkdir -p $(@D)
	$(CC) -MMD -c -o $@ $< $(CFLAGS)

$(BUILD_DIR)/%.o: %.S
	mkdir -p $(@D)
	$(AS) -o $@ $<

clean:
	-rm -rf $(BUILD_DIR)
